<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
    test(() => {
        var getter_called = false;
        var element = document.createElement("div");
        var element_style = element.style;
        var descriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "style");

        Object.defineProperty(element, "style", {
            get: function () {
                getter_called = true;
                return element_style;
            },
            set: descriptor.set
        });

        element.style = "color: green";

        assert_true(getter_called, "Overridden getter should be called");
        assert_equals(element_style.color, "green", "Put forwarding still works");
    }, "Overriding getter of [PutForwards] attribute");

    test(() => {
        var setter_called = false;
        var element = document.createElement("div");
        var element_style = element.style;
        var descriptor = Object.getOwnPropertyDescriptor(CSSStyleDeclaration.prototype, "cssText");

        Object.defineProperty(element_style, "cssText", {
            get: descriptor.get,
            set: function (v) {
                setter_called = true;
                descriptor.set.call(this, v);
            }
        });

        element.style = "color: green";

        assert_true(setter_called, "Overridden setter should be called");
        assert_equals(element_style.color, "green", "Put forwarding still works");
    }, "Overriding setter of [PutForwards] target attribute");

    test(() => {
        var element = document.createElement("div");
        var element_style = element.style;
        var fake_style = { cssText: "original" };
        var descriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "style");

        Object.defineProperty(element, "style", {
            get: function () {
                return fake_style;
            },
            set: descriptor.set
        });

        element.style = "color: green";

        assert_equals(element_style.cssText, "", "Original value intact");
        assert_equals(fake_style.cssText, "color: green", "Fake style object updated");
    }, "Overriding target of [PutForwards] attribute");

    test(() => {
        var element = document.createElement("div");
        var descriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "style");

        const exceptionString = "exception";
        Object.defineProperty(element, "style", {
            get: function () {
                throw exceptionString;
            },
            set: descriptor.set
        });

        assert_throws_exactly(exceptionString, () => element.style = "color: green");
    }, "Exception propagation from getter of [PutForwards] attribute");

    test(() => {
        var element = document.createElement("div");
        var element_style = element.style;
        var descriptor = Object.getOwnPropertyDescriptor(CSSStyleDeclaration.prototype, "cssText");

        const exceptionString = "exception";
        Object.defineProperty(element_style, "cssText", {
            get: descriptor.get,
            set: function (v) {
                throw exceptionString;
            }
        });

        assert_throws_exactly(exceptionString, () => element.style = "color: green");
    }, "Exception propagation from setter of [PutForwards] target attribute");

    test(() => {
        var element = document.createElement("div");
        var descriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "style");

        Object.defineProperty(element, "style", {
            get: function () {
                return null;
            },
            set: descriptor.set
        });

        assert_throws_js(TypeError, () => element.style = "color: green");
    }, "TypeError when getter of [PutForwards] attribute returns non-object");
</script>
